package edu.uts.reekie.designer.model.commands;

import org.eclipse.gef.commands.Command;

import edu.uts.reekie.designer.model.AbstractConnector;

public class SetConnectorLabelCommand extends Command {

	String oldLabel = null;
	String newLabel = null;
	
	int type = AbstractConnector.LABEL_BI_DIRECTIONAL;
	
	AbstractConnector connector;
	
	public AbstractConnector getConnector(){
		return connector;
	}
	
	public void setConnector(AbstractConnector c){
		this.connector =c ;
	}
	
	public SetConnectorLabelCommand(String newLabel, int type) {
		super("Set Connector Label"); 
		
		this.newLabel = newLabel;
		this.type = type;
	}
	
	public boolean canExecute() {
		if (connector==null)
			return false;
		
		switch (type){
		case AbstractConnector.LABEL_BI_DIRECTIONAL:
			String l = connector.getLabel();
			if (l!=null && l.equalsIgnoreCase(newLabel))
				return false;
			break;
		case AbstractConnector.LABEL_SOURCE_TO_TARGET:
			String st = connector.getSourceToTargetLabel(); 
			if (st!=null && st.equalsIgnoreCase(newLabel))
				return false;
			break;
		case AbstractConnector.LABEL_TARGET_TO_SOURCE:
			String ts = connector.getTargetToSourceLabel();
			if (ts!=null && ts.equalsIgnoreCase(newLabel))
				return false;
			break;
		}
		
		
		return true;
	}
	
	public void execute() {
		switch (type){
		case AbstractConnector.LABEL_BI_DIRECTIONAL:
			oldLabel = connector.getLabel();
			connector.setLabel(newLabel);
			break;
		case AbstractConnector.LABEL_SOURCE_TO_TARGET:
			oldLabel = connector.getSourceToTargetLabel();
			connector.setSourceToTargetLabel(newLabel);
			break;
		case AbstractConnector.LABEL_TARGET_TO_SOURCE:
			oldLabel = connector.getTargetToSourceLabel();
			connector.setTargetToSourceLabel(newLabel);
			break;
		}
		
	}
	
	
	public void undo() {
		switch (type){
		case AbstractConnector.LABEL_BI_DIRECTIONAL:
			connector.setLabel(oldLabel);
			break;
		case AbstractConnector.LABEL_SOURCE_TO_TARGET:
			connector.setSourceToTargetLabel(oldLabel);
			break;
		case AbstractConnector.LABEL_TARGET_TO_SOURCE:
			connector.setTargetToSourceLabel(oldLabel);
			break;
		}
	}
}
